//
//  SearchViewController.m
//  Cat
//
//  Created by lushitong on 2018/12/22.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "SearchViewController.h"
#import "HttpHomeManager.h"
#import "SearchTopView.h"
#import "HomeTodayCollectView.h"
#import "SearchResultView.h"
#import "HXTagsView.h"
#import "HXTagAttribute.h"
#import <libextobjc/EXTScope.h>
#import "FLLoadingView.h"

@interface SearchViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UITextField *searchTextFiled;
@property (nonatomic, strong) UIImageView *searcgImageView;
@property (nonatomic, strong) UIButton *cannelButton;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) SearchTopView *searchInfoView;

@property (nonatomic, strong) UIView *bottomInfoView;
@property (nonatomic, strong) UILabel *todayInfoLabel;
@property (nonatomic, strong) HomeTodayCollectView *toddayView;
@property (nonatomic, strong) HXTagsView *tagsView;

@property (nonatomic, strong) SearchResultView *searchView;
@property (nonatomic, strong) FLLoadingView *loadingView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.topTitleView.hidden = YES;
	[self request];
	[self loadAllSubviews];
	[self performSelector:@selector(searchAction) withObject:self afterDelay:1.0];
}

- (void)searchAction {
	if (self.keyWord.length == 0) {
		return;
	}
	self.searchTextFiled.text = self.keyWord;
	[self textFieldShouldReturn:self.searchTextFiled];
}

#pragma mark----- lazy init -----

- (void)cannelButtonAction {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)loadAllSubviews {
	
	[self.view addSubview:self.operView];
	[self.view addSubview:self.searchView];
	[self.view addSubview:self.bottomView];
	
	[self.operView addSubviews:@[self.backView,self.searchTextFiled,self.searcgImageView,self.cannelButton]];
	
	[self.bottomView addSubviews:@[self.searchInfoView,self.bottomInfoView]];
	
	[self.bottomInfoView addSubviews:@[self.todayInfoLabel,self.toddayView]];
	
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.top.equalTo(self.view);
		make.height.equalTo(NavigationBarHeight);
	}];
	
	[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(12.0));
		make.top.equalTo(self.view.mas_top).offset(kQKFitModuleDecimal(22) + StatusBarSafeInsetHeight);
		make.height.equalTo(@(28));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(53));
	}];
	
	[self.searcgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(18.0), kQKFitModuleDecimal(18.0)));
		make.left.equalTo(self.backView.mas_left).offset(kQKFitModuleDecimal(8.0));
		make.centerY.equalTo(self.backView.mas_centerY);
	}];
	
	[self.searchTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.searcgImageView.mas_right).offset(kQKFitModuleDecimal(6));
		make.centerY.equalTo(self.searcgImageView.mas_centerY);
		make.right.equalTo(self.backView.mas_right);
	}];
	
	[self.cannelButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.backView.mas_right);
		make.right.equalTo(self.view.mas_right);
		make.centerY.equalTo(self.backView.mas_centerY);
		make.height.equalTo(@(kQKFitModuleDecimal(28.0)));
	}];
	
	[self.searchInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.operView);
		make.top.equalTo(self.operView.mas_bottom);
		make.height.equalTo(kQKFitModuleDecimal(61 + 8 + 17));
	}];
	
	[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.searchInfoView);
		make.top.equalTo(self.searchInfoView.mas_bottom).offset(kQKFitModuleDecimal(8.0));
		make.bottom.equalTo(self.view.mas_bottom);
	}];
	
	[self.bottomInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.searchInfoView);
		make.top.equalTo(self.searchInfoView.mas_bottom).offset(kQKFitModuleDecimal(8.0));
		make.height.equalTo(@(kQKFitModuleDecimal(13 + 104 + 60 + 30)));
	}];
	
	[self.todayInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.searchInfoView.mas_left).offset(kQKFitModuleDecimal(15.0));
		make.top.equalTo(self.searchInfoView.mas_bottom).offset(kQKFitModuleDecimal(17.0));
	}];
	
	[self.toddayView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.bottomInfoView);
		make.top.equalTo(self.todayInfoLabel.mas_bottom).offset(kQKFitModuleDecimal(13.0));
		make.height.equalTo(kQKFitModuleDecimal(13 + 104 + 60));
	}];
}

- (void)request {
	[HttpHomeManager getSearchWithSuccess:^(SearchInfo *searchModel) {
		[self.searchInfoView setHotsWordArray:searchModel.hotWords];
		[self loadAllList:searchModel.hotWords];
		[self.toddayView setHotsArray:searchModel.grooms];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)loadAllList:(NSArray *)list {
	
	HXTagAttribute *arr = [HXTagAttribute new];
	arr.borderWidth = 0.0;
	arr.normalBackgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	arr.selectedBackgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	arr.titleSize = kQKFitFont(12);
	arr.textColor = COLOR_WITH_HEX(0x4C4947);
	arr.tagSpace = kQKFitModuleDecimal(2.0);
	self.tagsView.tagAttribute = arr;
	@weakify(self);
	self.tagsView.completion = ^(NSArray *selectTags,NSInteger currentIndex) {
		@strongify(self);
		self.keyWord = (NSString *)[list safeObjectAtIndex:currentIndex];
		[self performSelector:@selector(searchAction) withObject:self afterDelay:1.0];
	};
	self.tagsView.tags = list;
	[self.view addSubview:self.tagsView];
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField {
	[self.searchView sendRequestWithCommed:YES andKeyWord:textField.text];
	self.bottomView.hidden = YES;
	self.searchInfoView.hidden = NO;
	self.searchView.hidden = NO;
	self.tagsView.hidden = YES;
	return YES;
}

-(void)textField1TextChange:(UITextField *)textField{
	if (textField.text.length == 0) {
		self.bottomView.hidden = NO;
		self.searchInfoView.hidden = NO;
		self.searchView.hidden = YES;
		self.tagsView.hidden = NO;
	}
}

#pragma mark----- lazy init -----

- (UIView *)operView {
	if (!_operView) {
		_operView = [[UIView alloc] init];
		_operView.backgroundColor = [UIColor whiteColor];
	}
	return _operView;
}

- (UIView *)bottomView {
	if (!_bottomView) {
		_bottomView = [[UIView alloc] init];
	}
	return _bottomView;
}
- (UIView *)bottomInfoView {
	if (!_bottomInfoView) {
		_bottomInfoView = [[UIView alloc] init];
		_bottomInfoView.backgroundColor = [UIColor whiteColor];
	}
	return _bottomInfoView;
}

- (SearchTopView *)searchInfoView {
	if (!_searchInfoView) {
		_searchInfoView = [[SearchTopView alloc] init];
	}
	return _searchInfoView;
}

- (HomeTodayCollectView *)toddayView {
	if (!_toddayView) {
		UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
		flowLayout.itemSize = CGSizeMake(kQKFitModuleDecimal(104), kQKFitModuleDecimal(104 + 104));
		flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
		flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
		flowLayout.minimumLineSpacing = kQKFitModuleDecimal(8);
		_toddayView.backgroundColor = [UIColor whiteColor];
		_toddayView = [[HomeTodayCollectView alloc] initWithFrame:CGRectMake(0.0, 0.0, ScreenWidth , kQKFitModuleDecimal(13 + 104 + 60)) collectionViewLayout:flowLayout];
	}
	return _toddayView;
}

- (UILabel *)todayInfoLabel {
	if (!_todayInfoLabel) {
		_todayInfoLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(18)] textColor:COLOR_WITH_HEX(0x57524F)];
		_todayInfoLabel.text = @"今日推荐";
	}
	return _todayInfoLabel;
}

- (UIView *)backView {
	if (!_backView){
		_backView = [[UIView alloc] init];
		_backView.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	}
	return _backView;
}

- (UIImageView *)searcgImageView {
	if (!_searcgImageView) {
		_searcgImageView = [[UIImageView alloc] init];
		_searcgImageView.image = [UIImage imageNamed:@"home_search"];
		_searcgImageView.userInteractionEnabled = YES;
	}
	return _searcgImageView;
}

- (UITextField *)searchTextFiled {
	if (!_searchTextFiled) {
		_searchTextFiled = [[UITextField alloc] init];
		_searchTextFiled.placeholder = @"搜索宝贝 领券后再购物";
		_searchTextFiled.clearButtonMode = UITextFieldViewModeUnlessEditing;
		_searchTextFiled.textColor = COLOR_WITH_HEX(0xCDC7C4);
		_searchTextFiled.font = [UIFont systemFontOfSize:kQKFitFont(12)];
		_searchTextFiled.returnKeyType = UIReturnKeySearch;
		_searchTextFiled.delegate = self;
		[_searchTextFiled addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
	}
	return _searchTextFiled;
}

- (UIButton *)cannelButton {
	if (!_cannelButton) {
		_cannelButton = [UIButton buttonWithTitle:@"取消" color:COLOR_WITH_HEX(0x8C939C) font:[UIFont systemFontOfSize:kQKFitFont(12)]];
		[_cannelButton addTarget:self action:@selector(cannelButtonAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _cannelButton;
}

- (SearchResultView *)searchView {
	if (!_searchView) {
		_searchView = [[SearchResultView alloc] init];
		UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
		layout.minimumLineSpacing = 0.0;
		layout.minimumInteritemSpacing = kQKFitModuleFloor(10);
		_searchView = [[SearchResultView alloc] initWithFrame:CGRectMake(0.0, NavigationBarHeight, ScreenWidth, ScreenHeight - NavigationBarHeight) layout:layout];
		_searchView.hidden = YES;
	}
	return _searchView;
}

- (HXTagsView *)tagsView {
	if (!_tagsView) {
		_tagsView =  [[HXTagsView alloc] initWithFrame:CGRectMake(kQKFitModuleDecimal(12.0),NavigationBarHeight + kQKFitModuleDecimal(30), ScreenWidth, kQKFitModuleDecimal(60))];
	}
	return _tagsView;
}


- (FLLoadingView *)loadingView {
	if (!_loadingView) {
		_loadingView = [[FLLoadingView alloc] init];
		_loadingView.backgroundColor = [UIColor whiteColor];
	}
	return _loadingView;
}


@end
